'use strict';


const Controller = require('egg').Controller;
const SortWord = require('sort-word');
const friend = require('../model/friend');


class FriendController extends Controller {
    async list() {
        const currentuserid = this.ctx.authUser.id;
        // this.ctx.body="OK"
        let friend = await this.app.model.Friend.findAndCountAll({
            where: {
                user_id: currentuserid

            },
            include: [{

                model: this.app.model.User,
                as: "friendInfo",
                attributes: ["id", "username", "nickname", "avatar"],
            }],
        })
        let res = friend.rows.map(item => {
            let name = item.friendInfo.nickname ? item.friendInfo.nickname : item.friendInfo.username
            if (item.nickname) {
                name = item.nickname
            }
            return {
                id: item.id,
                user_id: item.friendInfo.id,
                name: name,
                username: item.friendInfo.username,
                avatar: item.friendInfo.avatar
            }
        })
        console.log(res)
        if(res.length>0){
            friend.rows = new SortWord(res, "name");

        }
        
        this.ctx.apiSuccess(friend, "获取成功", 200)


    }
    //查看好友资料
    async read() {
        const currentuserid = this.ctx.authUser.id;
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        let user = await this.app.model.User.findOne({
            where: {
                id: id,
                status: 1
            },
            attributes: {
                exclude: ["password"]
            }
        })
        if (!user) {
            this.ctx.throw(400, "用户不存在")
        }
        let ress = {
            id: user.id,
            username: user.username,
            nickname: user.nickname ? user.nickname : user.username,
            avatar: user.avatar,
            sex: user.sex,
            sign: user.sign,
            area: user.area,
            friend: false
        }

        let res = await this.app.model.Friend.findOne({
            where: {
                friend_id: id,
                user_id: currentuserid
            },
            include: [{

                // model: this.app.model.User,
                // as: "friendInfo",
                // attributes: ["id", "username", "nickname", "avatar"],
                
                model: this.app.model.Tag,
                attributes: ["name"],
            }],
        })
        if (res) {
            ress.friend = true
            if (res.nickname) {
                ress.nickname = res.nickname
            }
            ress = { ...ress, lookme: res.lookme, lookhim: res.lookhim, star: res.star, isblack: res.isblack, tags: res.tags.map(item => item.name) }
        }
        this.ctx.apiSuccess(ress, "获取成功", 200)
        // this.ctx.apiSuccess(res, "获取成功", 200)
        // this.ctx.body = (res,"获取成功",200)
    }
    async setblack() {
        const currentuserid = this.ctx.authUser.id;
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        this.ctx.validate({
            isblack: {
                type: "int", required: true, range: {
                    in: [0, 1]
                }, desc: "是否拉黑"
            },
        });
        let res = await this.app.model.Friend.findOne({
            where: {
                friend_id: id,
                user_id: currentuserid
            }
        })
        if (!res) {
            this.ctx.throw(400, "该用户不存在")
        }
        res.isblack = this.ctx.request.body.isblack;
        res.save()
        this.ctx.apiSuccess(res, "修改成功", 200)
    }
    async setstar() {
        const currentuserid = this.ctx.authUser.id;
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        this.ctx.validate({
            star: {
                type: "int", required: true, range: {
                    in: [0, 1]
                }, desc: "是否星标"
            },
        });
        let res = await this.ctx.model.Friend.findOne({
            where: {
                friend_id: id,
                user_id: currentuserid,
                isblack: 0
            }
        })
        if (!res) {
            this.ctx.throw(400, "该用户不存在")
        }
        res.star = this.ctx.request.body.star;
        res.save()
        this.ctx.apiSuccess(res, "修改成功", 200);

    }
    async setmomentauth() {
        const currentuserid = this.ctx.authUser.id;
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        this.ctx.validate({
            lookhim: {
                type: "int", required: true, range: {
                    in: [0, 1]
                }, desc: "是否看他"
            },
            lookme: {
                type: "int", required: true, range: {
                    in: [0, 1]
                }, desc: "是否允许他看我"
            },
        });
        console.log(id,"id")
        let res = await this.ctx.model.Friend.findOne({
            where:{
                friend_id: id,
                user_id: currentuserid,
                isblack: 0
            
            }
            })
        console.log(res,"用户")
        if (!res) {
            this.ctx.throw(400, "该用户不存在")

        }
        res.lookme = this.ctx.request.body.lookme
        res.lookhim = this.ctx.request.body.lookhim
        res.save();
        this.ctx.apiSuccess(res, "设置成功", 200)


    }
    async setrearktag() {
        const currentuserid = this.ctx.authUser.id;
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        this.ctx.validate({
            nickname: {
                type: "string", required: false, desc: "昵称"
            },
            tags: {
                type: "string", required: true, desc: "标签"
            },
        });
        let user = await this.app.model.Friend.findOne({
            where: {
                user_id: currentuserid,
                friend_id: id,
                isblack: 0
            },
            include: [{
                model: this.app.model.Tag
            }]

        })
        if (!user) {
            this.ctx.throw(404, "该用户不存在")

        }
        user.nickname = this.ctx.request.body.nickname;
        await user.save()
        let allTags = await this.app.model.Tag.findAll({
            where: {
                user_id: currentuserid
            }
        })
        let allTagsName = allTags.map(item => item.name)


        let tags = this.ctx.request.body.tags.split(",");
        console.log(tags, "tags")

        let addtags = tags.filter(item => !allTagsName.includes(item));
        addtags = addtags.map(item => {
            return {
                name: item,
                user_id: currentuserid

            }
        })
        let res = await this.app.model.Tag.bulkCreate(addtags);
        let alltags = await this.app.model.Tag.findAll({
            where: {
                user_id: currentuserid,
                name: tags
            }
        })
        let oldtagsId = user.tags.map(item => item.id);

        let newtagsID = alltags.map(item => item.id);
        let addTagsIDs = newtagsID.filter(id => !oldtagsId.includes(id));
        let delTagsIDs = oldtagsId.filter(id => !newtagsID.includes(id));

        let addtsgsids = addTagsIDs.map(id => {

            return {
                tag_id: id,
                friend_id: user.id
            }

        })
        let res1 = await this.app.model.FriendTag.bulkCreate(addtsgsids);
        this.app.model.FriendTag.destroy({
            where: {
                tag_id: delTagsIDs,
                friend_id: user.id
            }
        })
        this.ctx.apiSuccess(res1)

    }
}

module.exports = FriendController;
